package com.qztc.hire_end_core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qztc.hire_end_core.mapper.NoticeTypeMapper;
import com.qztc.hire_end_dto.dto.NoticeType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/noticeType")
@Tag(name = "公告类型管理接口")
public class NoticeTypeController {
    @Autowired
    private NoticeTypeMapper noticeTypeMapper;

    @Operation(summary = "查询所有公告类型")
    @GetMapping("/getAll")
    public List<NoticeType> getAll() {
        return new NoticeType().selectAll();
    }

    @Operation(summary = "根据noticeTypeId获取具体公告类型")
    @GetMapping("/getbynoticeTypeId")
    public NoticeType getbynoticeTypeId(@RequestParam Integer noticeTypeId) {
        QueryWrapper<NoticeType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_typeid", noticeTypeId);
        return new NoticeType().selectOne(queryWrapper);
    }

    @Operation(summary = "根据noticeTypeId删除某一个具体公告类型")
    @DeleteMapping("/delete")
    public boolean delete(@RequestBody NoticeType noticeType) {
        return noticeType.deleteById(noticeType.getNoticeTypeId());
    }

    @Operation(summary = "根据noticeTypeId修改某一个具体公告类型")
    @PutMapping("/update")
        public boolean update(@RequestBody NoticeType noticeType) {
        QueryWrapper<NoticeType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_typename", noticeType.getNoticeTypeName());
        if(noticeType.selectCount(queryWrapper) > 0) {
            return false;
        }
        return noticeType.updateById();
    }

    @Operation(summary = "增加公告类型")
    @PostMapping("/add")
    public boolean add(@RequestBody NoticeType noticeType) {
        QueryWrapper<NoticeType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_typename", noticeType.getNoticeTypeName());
        if(noticeType.selectCount(queryWrapper) > 0) {
            return false;
        }
        return noticeType.insert();
    }

    @Operation(summary = "批量删除公告类型")
    @DeleteMapping("/deleteMany")
    public boolean deleteMany(@RequestBody List<Integer> noticeTypeIds) {
        if (noticeTypeIds == null || noticeTypeIds.isEmpty()) {
            return false;
        }
        QueryWrapper<NoticeType> wrapper = new QueryWrapper<>();
        wrapper.in("notice_typeid", noticeTypeIds);
        return new NoticeType().delete(wrapper);
    }

    @Operation(summary = "批量增加公告类型")
    @PostMapping("/addMany")
    public boolean addMany(@RequestBody List<NoticeType> noticeTypes) {
        if (noticeTypes == null || noticeTypes.isEmpty()) {
            return false;
        }
        // 批量重复校验
        QueryWrapper<NoticeType> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("notice_typename",
                noticeTypes.stream().map(NoticeType::getNoticeTypeName).collect(Collectors.toList()));
        if (noticeTypeMapper.selectCount(queryWrapper) > 0) {
            return false;
        }
        // 批量插入SQL
        String sql = "insert into notice_type (notice_typeid, notice_typename) values" +
                noticeTypes.stream()
                        .map(type -> String.format("(%d,'%s')", type.getNoticeTypeId(), type.getNoticeTypeName()))
                        .collect(Collectors.joining(","));
        return noticeTypeMapper.executeSql(sql);
    }
}